package com.ffdms.mobileoffice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ffdms.util.MobileOfficeUtil;
import com.ffdms.vo.ProjectInfo;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ProjectListActivity extends BaseActivity implements
		OnClickListener, OnItemClickListener {
	private ListView mProjectListView;
	// widgets
	private Button mMenu;
	private PopupWindow mPopupWindow;
	private ListView mMenuListview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_project_list);
		init();
	}

	private void init() {
		mProjectListView = (ListView) findViewById(R.id.listview);
		mMenu = (Button) findViewById(R.id.project_menu);
		mMenu.setOnClickListener(this);
		populateProjects(0);
	}

	private void populateProjects(int type) {
		final List<ProjectInfo> projects = new ArrayList<ProjectInfo>();
		ProjectInfo n = new ProjectInfo();
		n.setDescription("本报讯 （记者徐海星、张涨 通讯员郑仁鹏、黄少江、陈建族）同饮珠江水，保护母亲河。昨天下午2时，2013年广州横渡珠江活动在中大码头举行。来自广州、佛山、肇庆、清远、东莞、中山等6市的2000名横渡健儿组成了40个方队依次下水，在热烈的加油欢呼声中游抵位于二沙岛星海音乐厅附近的登陆点。广州市委书记万庆良、市长陈建华，市领导陈国、王世彤、欧阳卫民、平欣光，市政府秘书长周亚伟;佛山市市长刘悦伦，市领导林征、赖紫宁，以及肇庆、清远、东莞、中山等市有关游泳协会负责人分别率领各市方队成功横渡。");
		n.setName("项目一");
		n.setiD(1);
		n.setEnterpriseID(1);
		n.setModifyTime(new Date());
		projects.add(n);

		ProjectInfo n1 = new ProjectInfo();
		n1.setDescription("本报讯 （记者徐海星、张涨 通讯员郑仁鹏、黄少江、陈建族）同饮珠江水，保护母亲河。昨天下午2时，2013年广州横渡珠江活动在中大码头举行。来自广州、佛山、肇庆、清远、东莞、中山等6市的2000名横渡健儿组成了40个方队依次下水，在热烈的加油欢呼声中游抵位于二沙岛星海音乐厅附近的登陆点。广州市委书记万庆良、市长陈建华，市领导陈国、王世彤、欧阳卫民、平欣光，市政府秘书长周亚伟;佛山市市长刘悦伦，市领导林征、赖紫宁，以及肇庆、清远、东莞、中山等市有关游泳协会负责人分别率领各市方队成功横渡。");
		n1.setName("项目二");
		n1.setiD(1);
		n1.setEnterpriseID(1);
		n1.setModifyTime(new Date());
		projects.add(n1);

		ProjectInfo n2 = new ProjectInfo();
		n2.setDescription("本报讯 （记者徐海星、张涨 通讯员郑仁鹏、黄少江、陈建族）同饮珠江水，保护母亲河。昨天下午2时，2013年广州横渡珠江活动在中大码头举行。来自广州、佛山、肇庆、清远、东莞、中山等6市的2000名横渡健儿组成了40个方队依次下水，在热烈的加油欢呼声中游抵位于二沙岛星海音乐厅附近的登陆点。广州市委书记万庆良、市长陈建华，市领导陈国、王世彤、欧阳卫民、平欣光，市政府秘书长周亚伟;佛山市市长刘悦伦，市领导林征、赖紫宁，以及肇庆、清远、东莞、中山等市有关游泳协会负责人分别率领各市方队成功横渡。");
		n2.setName("项目三");
		n2.setiD(1);
		n2.setEnterpriseID(1);
		n2.setModifyTime(new Date());
		projects.add(n2);

		ProjectInfo n3 = new ProjectInfo();
		n3.setDescription("本报讯 （记者徐海星、张涨 通讯员郑仁鹏、黄少江、陈建族）同饮珠江水，保护母亲河。昨天下午2时，2013年广州横渡珠江活动在中大码头举行。来自广州、佛山、肇庆、清远、东莞、中山等6市的2000名横渡健儿组成了40个方队依次下水，在热烈的加油欢呼声中游抵位于二沙岛星海音乐厅附近的登陆点。广州市委书记万庆良、市长陈建华，市领导陈国、王世彤、欧阳卫民、平欣光，市政府秘书长周亚伟;佛山市市长刘悦伦，市领导林征、赖紫宁，以及肇庆、清远、东莞、中山等市有关游泳协会负责人分别率领各市方队成功横渡。");
		n3.setName("项目四");
		n3.setiD(1);
		n3.setEnterpriseID(1);
		n3.setModifyTime(new Date());
		projects.add(n3);

		ProjectListAdapter adp = new ProjectListAdapter(this, projects);
		mProjectListView.setAdapter(adp);
		mProjectListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				ProjectInfo n = projects.get(arg2);
				Intent t = new Intent(ProjectListActivity.this,
						NoticeActivity.class);
				t.putExtra(
						"time",
						MobileOfficeUtil.formatDate("yyyy年MM月dd日",
								n.getModifyTime()));
				t.putExtra("content", n.getDescription());
				startActivity(t);
			}
		});
	}

	@Override
	public void onClick(View v) {
		showMenuPopUp();
	}

	private void showMenuPopUp() {
		View view = LayoutInflater.from(this).inflate(
				R.layout.project_menu_custom_view, null);
		mMenuListview = (ListView) view.findViewById(R.id.listview);
		mMenuListview.setAdapter(new MenuAdapter(getResources().getStringArray(
				R.array.project_menu_array)));
		mMenuListview.setOnItemClickListener(this);
		mPopupWindow = new PopupWindow(view, getResources().getInteger(
				R.integer.project_menu_popup_width), LayoutParams.WRAP_CONTENT);
		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(false);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow.showAsDropDown(mMenu, -5, 0);
	}

	class MenuAdapter extends BaseAdapter {
		private String[] mMenuArray;

		public MenuAdapter(String[] menuArray) {
			// TODO Auto-generated constructor stub
			mMenuArray = menuArray;
		}

		@Override
		public int getCount() {
			return mMenuArray.length;
		}

		@Override
		public Object getItem(int arg0) {
			return mMenuArray[arg0];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(ProjectListActivity.this)
						.inflate(R.layout.project_menu_item_view, null);
				holder = new ViewHolder();
				holder.menuIcon = (ImageView) convertView
						.findViewById(R.id.project_menu_item_icon);
				holder.menuTitle = (TextView) convertView
						.findViewById(R.id.project_menu_item_title);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			switch (position) {
			case 0:
				holder.menuIcon
						.setBackgroundResource(R.drawable.project_menu_all_selector);
				break;
			case 1:
				holder.menuIcon
						.setBackgroundResource(R.drawable.project_menu_current_selector);
				break;
			case 2:
				holder.menuIcon
						.setBackgroundResource(R.drawable.project_menu_end_selector);
				break;
			case 3:
				holder.menuIcon
						.setBackgroundResource(R.drawable.project_menu_review_selector);
				break;
			case 4:
				holder.menuIcon
						.setBackgroundResource(R.drawable.project_menu_add_selector);
				break;
			case 5:
				holder.menuIcon
						.setBackgroundResource(R.drawable.project_menu_delete_selector);
				break;
			default:
				break;
			}
			holder.menuTitle.setText(mMenuArray[position]);
			return convertView;
		}

		class ViewHolder {
			ImageView menuIcon;
			TextView menuTitle;
		}
	}

	private void closeMenuPopUp() {
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg0.getAdapter() instanceof MenuAdapter) {
			closeMenuPopUp();
		}
	}
}
